package com.wgts.wgts.control.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/*
 *
 *   data:2018/9/1110:11
 *
 */public class PayDialogFragment extends AbstractCustomDialogFragment {
    @BindView(R.id.need_pay_price)
    TextView needPayPrice;
    @BindView(R.id.wx_pay_image)
    ImageView wxPayImage;
    @BindView(R.id.wx_pay_ly)
    LinearLayout wxPayLy;
    @BindView(R.id.alipay_pay_image)
    ImageView alipayPayImage;
    @BindView(R.id.alipay_pay_ly)
    LinearLayout alipayPayLy;
    @BindView(R.id.affirm_order_residue_money)
    TextView affirmOrderResidueMoney;
    @BindView(R.id.mohu_pay_image)
    ImageView mohuPayImage;
    @BindView(R.id.mohu_pay_ly)
    LinearLayout mohuPayLy;
    @BindView(R.id.dialog_pay_button)
    LinearLayout dialogPayButton;
    @BindView(R.id.cancel_dialog)
    ImageView cancelDialog;
    Unbinder unbinder;
    private View view;
    //支付方式0微信，1支付宝，2
    private Integer choosePay = 0;
    private payCallBack payCallBack;


    @Override
    public void onStart() {
        super.onStart();
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.BOTTOM;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);
    }

    @Override
    public View onCreateDialogView() {
        view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_fragment_pay, null);
        unbinder = ButterKnife.bind(this, view);
        wxPayImage.setSelected(true);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.wx_pay_ly, R.id.alipay_pay_ly, R.id.mohu_pay_ly, R.id.dialog_pay_button, R.id.cancel_dialog})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wx_pay_ly:
                choosePayType(0);
                break;
            case R.id.alipay_pay_ly:
                choosePayType(1);
                break;
            case R.id.mohu_pay_ly:
                choosePayType(2);
                break;
            case R.id.dialog_pay_button:
                if (payCallBack != null){
                    payCallBack.callBack(choosePay);
                    dismiss();
                }
                break;
            case R.id.cancel_dialog:
                dismiss();
                break;
        }
    }

    /*
     * 选择支付方式
     * */
    private void choosePayType(Integer type){
        switch (type){
            case 0:
                choosePay = 0;
                wxPayImage.setSelected(true);
                alipayPayImage.setSelected(false);
                mohuPayImage.setSelected(false);
                break;
            case 1:
                choosePay = 1;
                wxPayImage.setSelected(false);
                alipayPayImage.setSelected(true);
                mohuPayImage.setSelected(false);
                break;
            case 2:
                choosePay = 2;
                wxPayImage.setSelected(false);
                alipayPayImage.setSelected(false);
                mohuPayImage.setSelected(true);
                break;
            default:
        }
    }

    public void pay(payCallBack payCallBack){
        this.payCallBack = payCallBack;
    }

    public interface payCallBack{
        void callBack(int payType);
    }
}
